package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.News;
import org.example.system.entity.Respond;
import org.example.system.entity.OperateLog;
import org.example.system.entity.User;
import org.example.system.mapper.RespondMapper;
import org.example.system.service.RespondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 响应服务层实现类
 */
@Service
public class RespondServiceImpl implements RespondService {
    @Autowired
    private RespondMapper respondMapper;

    /**
     * 按条件分页查询
     * @param respond
     * @return
     */
    @Override
    public IPage<Respond> selectPage(Respond respond){
        MPJLambdaWrapper<Respond> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Respond.class);
        wrapper.leftJoin(News.class, News::getId,Respond::getNewsId);
        wrapper.leftJoin(User.class, User::getId,Respond::getUserId);
        //条件
        if (respond.getNewsId() != null){
            wrapper.eq(Respond::getNewsId,respond.getNewsId());
        }
        if (respond.getUserId() != null){
            wrapper.eq(Respond::getUserId,respond.getUserId());
        }
        wrapper.selectAs(News::getTitle,Respond::getNewsTitle);
        wrapper.selectAs(User::getRealname,Respond::getRealname);
        wrapper.selectAs(User::getAvatar,Respond::getAvatar);
        wrapper.selectAs(User::getRole,Respond::getRole);
        if (StrUtil.isNotEmpty(respond.getNewsTitle())){
            wrapper.like(News::getTitle,respond.getNewsTitle());    //相似的新闻标题（模糊查询）
        }
        if (StrUtil.isNotEmpty(respond.getCreateBy())){
            wrapper.like(Respond::getCreateBy,respond.getCreateBy());    //相似的发布用户名（模糊查询）
        }

        if (StrUtil.isNotEmpty(respond.getRealname())){
            wrapper.like(User::getRealname,respond.getRealname());    //相似的真实姓名（模糊查询）
        }
        if (respond.getStatus() != null) {
            wrapper.eq(Respond::getStatus, respond.getStatus());    //审核状态查询（精确查询）
        }
        wrapper.orderByDesc(OperateLog::getId);
        IPage<Respond> page = new Page<>();
        page.setCurrent(respond.getPageNum());
        page.setSize(respond.getPageSize());
        return respondMapper.selectPage(page,wrapper);
    }

    /**
     * 添加响应
     * @param respond
     * @return
     */
    @Override
    public int add(Respond respond) {
        respond.setCreateTime(new Date());
        return respondMapper.insert(respond);
    }

    /**
     * 修改
     * @param respond
     * @return
     */
    @Override
    public int update(Respond respond) {
        respond.setUpdateTime(new Date());
        return respondMapper.updateById(respond);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return respondMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Respond getOne(Long id) {
        return respondMapper.selectById(id);
    }
}
